package sort;

import java.util.Arrays;

/**
 * 描述:合并两个有序的数组为一个新的有序数组
 *
 * @author
 * @created 2022/1/11 19:11
 */
public class MergeSortedArray {

    public static void main(String[] args) {
        int[] ints1 = {1, 3, 7};
        int[] ints2 = {2, 9, 10};
        int[] ints = mergeSortedArray(ints1, ints2);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] mergeSortedArray(int[] arr1, int[] arr2) {
        int i = 0;
        int j = 0;
        int t = 0;
        int[] temp = new int[arr1.length + arr2.length];
        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] >= arr2[j]) {
                temp[t++] = arr2[j++];
            } else {
                temp[t++] = arr1[i++];
            }
        }
        while (i < arr1.length) {
            temp[t++] = arr1[i++];
        }
        while (j < arr2.length) {
            temp[t++] = arr2[j++];
        }
        return temp;
    }

}
